package com.wnx.job.modules.rms.controller;

import com.wnx.job.common.api.Const;
import com.wnx.job.common.api.ResultData;
import com.wnx.job.common.mapstruct.CertificateMapper;
import com.wnx.job.modules.jms.model.JmsStudent;
import com.wnx.job.modules.jms.service.JmsStudentService;
import com.wnx.job.modules.pms.service.PmsResumeService;
import com.wnx.job.modules.rms.dto.CertificateParam;
import com.wnx.job.modules.rms.dto.ResumeParam;
import com.wnx.job.modules.rms.model.RmsCertificate;
import com.wnx.job.modules.rms.service.RmsCertificateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.security.Principal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangnaixing
 * @since 2021-12-17
 */
@Slf4j
@RestController
@RequestMapping("/rms/rmsCertificate")
@Api(tags = "CertificateController",description = "荣誉证书管理")
public class RmsCertificateController {

    @Resource
    private JmsStudentService studentService;

    @Resource
    private PmsResumeService resumeService;

    @Resource
    private RmsCertificateService certificateService;

    /**
     * 新增荣誉证书
     * @param certificateParam
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增荣誉证书")
    public ResultData<String> save(@RequestBody @Validated CertificateParam certificateParam, Principal principal){
        log.info("=======系统正在新增荣誉证书======");
        //获取前端的荣誉证书对象

        //属性复制
        RmsCertificate tbCertificate = CertificateMapper.INSTANCE.toPojo(certificateParam);


        //设置简历ID进荣誉证书
        String account = principal.getName();
        JmsStudent student = studentService.findByAccount(account);
        ResumeParam resume = resumeService.findByStudentId(student.getId());
        tbCertificate.setResumeId(resume.getId());

        //调用服务层，进行新增荣誉证书
        certificateService.save(tbCertificate);

        log.info("=======系统正在新增荣誉证书成功！======");
        return ResultData.success(Const.ADD_CERTIFICATE_SUCCESS);


    }

    /**
     * 删除荣誉证书
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @ApiOperation("删除荣誉证书")
    public ResultData<String> delete(@PathVariable(name = "id")Long id){
        log.info("========系统正在删除荣誉证书=====");
        //获取前端的荣誉证书ID

        //调用服务层，根据ID删除荣誉证书
        certificateService.removeById(id);

        //返回结果集对象

        log.info("========系统删除荣誉证书成功！=====");
        return ResultData.success(Const.DELETE_CERTIFICATE_SUCCESS);
    }

    /**
     * 查询荣誉证书
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @ApiOperation("查询荣誉证书")
    public ResultData<RmsCertificate> findById(@PathVariable(name = "id")Long id){
        log.info("========系统正在删除查询荣誉证书=====");
        //获取前端的荣誉证书ID

        //调用服务层，根据ID查询荣誉证书
        RmsCertificate certificate = certificateService.getById(id);
        log.info("========系统查询荣誉证书成功！=====");
        return ResultData.success(Const.QUERY_CERTIFICATE_SUCCESS,certificate);

    }

    /**
     * 修改荣誉证书
     * @param certificateParam
     * @param principal
     * @return
     */
    @PostMapping("/update")
    @ApiOperation("修改荣誉证书")
    public ResultData<String> update(@Valid @RequestBody CertificateParam certificateParam, Principal principal){
        log.info("========系统正在修改荣誉证书！=====");

        //获取前端的荣誉证书对象

        //属性复制
        RmsCertificate tbCertificate = CertificateMapper.INSTANCE.toPojo(certificateParam);


        //设置简历ID进荣誉证书
        String account = principal.getName();
        JmsStudent student = studentService.findByAccount(account);
        ResumeParam resume = resumeService.findByStudentId(student.getId());
        tbCertificate.setResumeId(resume.getId());

        //调用服务层，进行修改荣誉证书
        certificateService.updateById(tbCertificate);

        log.info("========系统正在修改荣誉证书成功！！！=====");
        //返回结果集
        return ResultData.success(Const.UPDATE_CERTIFICATE_SUCCESS);


    }


    /**
     * 查询全部荣誉证书
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询全部荣誉证书")
    public ResultData<List<RmsCertificate>> findAll(Principal principal){
        log.info("=====系统正在查询全部荣誉证书=====");
        //通过认证主体，获取学生学号。
        String account = principal.getName();

        //调用服务层，根据学生学号获取学生信息
        JmsStudent student = studentService.findByAccount(account);

        //调用服务层，根据学生ID获取简历
        ResumeParam resume = resumeService.findByStudentId(student.getId());

        //调用服务层进行根据简历ID获取学生荣誉证书集合
        List<RmsCertificate> certificateList = certificateService.findCertificateListByResumeId(resume.getId());

        log.info("=====系统正在查询全部荣誉证书成功！=====");
        //返回结果集对象
        return ResultData.success(Const.QUERY_CERTIFICATE_SUCCESS,certificateList);
    }
}

